package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.InspectionArrivalChildOrder;
import com.sevattal.service.leaves.entity.InspectionArrivalMainOrder;
import com.sevattal.service.leaves.entity.vo.InspectionArrivalChildOrderVo;
import com.sevattal.service.leaves.entity.vo.InspectionArrivalMainOrderVo;
import com.sevattal.service.leaves.entity.vo.InspectionArrivalOrderVo;
import com.sevattal.service.leaves.mapper.InspectionArrivalChildOrderMapper;
import com.sevattal.service.leaves.mapper.InspectionArrivalMainOrderMapper;
import com.sevattal.service.leaves.service.InspectionArrivalOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  检验到货主表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class InspectionArrivalOrderServiceImpl implements InspectionArrivalOrderService {

    @Autowired
    private InspectionArrivalMainOrderMapper mainOrderMapper;

    @Autowired
    private InspectionArrivalChildOrderMapper childOrderMapper;

    @Override
    public boolean createInspectionOrder(InspectionArrivalOrderVo inspectionOrderVo) {
        JudgmentData(inspectionOrderVo);
        QueryWrapper<InspectionArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_iamId",inspectionOrderVo.getFIamid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count > 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.insert(getMainOrder(inspectionOrderVo));
        List<InspectionArrivalChildOrder> childOrderList = getChildOrder(inspectionOrderVo);
        for (int i=0;i<childOrderList.size();i++){
            childOrderMapper.insert(childOrderList.get(i));
        }
        return true;
    }

    @Override
    public boolean updateInspectionOrder(InspectionArrivalOrderVo inspectionOrderVo) {
        JudgmentData(inspectionOrderVo);
        QueryWrapper<InspectionArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_iamId",inspectionOrderVo.getFIamid());
        int count = mainOrderMapper.selectCount(mainOrderQueryWrapper);
        if (count == 0){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        mainOrderMapper.update(getMainOrder(inspectionOrderVo),mainOrderQueryWrapper);

        QueryWrapper<InspectionArrivalChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_iamId",inspectionOrderVo.getFIamid());
        childOrderMapper.delete(childOrderQueryWrapper);
        List<InspectionArrivalChildOrder> childOrderList = getChildOrder(inspectionOrderVo);
        for (int i=0;i<childOrderList.size();i++){
            childOrderMapper.insert(childOrderList.get(i));
        }
        return true;
    }

    @Override
    public boolean deleteInspectionOrder(String id) {
        QueryWrapper<InspectionArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_iamId",id);
        mainOrderMapper.delete(mainOrderQueryWrapper);
        QueryWrapper<InspectionArrivalChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        childOrderQueryWrapper.eq("f_iamId",id);
        childOrderMapper.delete(childOrderQueryWrapper);
        return true;
    }

    @Override
    public List<InspectionArrivalOrderVo> getInspectionOrderList(InspectionArrivalOrderVo inspectionOrderVo) {
        QueryWrapper<InspectionArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.orderByAsc("iam.f_iamId");
        mainOrderQueryWrapper.eq("iam.is_disabled",0);
        mainOrderQueryWrapper.eq("iam.is_deleted",0);
        if (inspectionOrderVo.getFIamid() != null && !StringUtils.isEmpty(inspectionOrderVo.getFIamid())){
            mainOrderQueryWrapper.like("iam.f_iamId",inspectionOrderVo.getFIamid());
        }
        if (inspectionOrderVo.getFEname() != null && !StringUtils.isEmpty(inspectionOrderVo.getFEname())){
            mainOrderQueryWrapper.like("e.f_eName",inspectionOrderVo.getFEname());
        }
        if (inspectionOrderVo.getFIamstatus() != null && !StringUtils.isEmpty(inspectionOrderVo.getFIamstatus())){
            mainOrderQueryWrapper.eq("iam.f_iamStatus",inspectionOrderVo.getFIamstatus());
        }
        if (inspectionOrderVo.getSearchStartDate() != null){
            mainOrderQueryWrapper.ge("iam.f_iamDate",inspectionOrderVo.getSearchStartDate());
        }
        if (inspectionOrderVo.getSearchEndDate() != null) {
            mainOrderQueryWrapper.le("iam.f_iamDate",inspectionOrderVo.getSearchEndDate());
        }
        List<InspectionArrivalMainOrderVo> mainOrders = mainOrderMapper.getInspectionMainOrderList(mainOrderQueryWrapper);

        List<InspectionArrivalOrderVo> orderVoList = new ArrayList<>();
        QueryWrapper<InspectionArrivalChildOrder> childOrderQueryWrapper = new QueryWrapper<>();
        for (int i=0;i<mainOrders.size();i++){
            childOrderQueryWrapper.eq("iac.is_disabled",0);
            childOrderQueryWrapper.eq("iac.is_deleted",0);
            childOrderQueryWrapper.eq("iac.f_iamId",mainOrders.get(i).getFIamid());
            List<InspectionArrivalChildOrderVo> childOrderVos = childOrderMapper.getInspectionChildOrderList(childOrderQueryWrapper);
            orderVoList.add(getVo(mainOrders.get(i),childOrderVos));
            childOrderQueryWrapper.clear();
        }
        return orderVoList;
    }

    @Override
    public boolean confirmInspectionOrder(String id) {
        QueryWrapper<InspectionArrivalMainOrder> mainOrderQueryWrapper = new QueryWrapper<>();
        mainOrderQueryWrapper.eq("f_iamId",id);
        InspectionArrivalMainOrder mainOrder = new InspectionArrivalMainOrder();
        mainOrder.setFIamconfirm("1");
        mainOrderMapper.update(mainOrder,mainOrderQueryWrapper);
        return true;
    }

    // 判断数据是否正确
    public void JudgmentData(InspectionArrivalOrderVo orderVo){
        if (orderVo.getInspectionArrivalChildOrderVoList().size() == 0 || orderVo.getFIamid() == null || StringUtils.isEmpty(orderVo.getFIamid())){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
    }

    // 获取主表数据
    public InspectionArrivalMainOrder getMainOrder(InspectionArrivalOrderVo orderVo){
        InspectionArrivalMainOrder mainOrder = new InspectionArrivalMainOrder();
        mainOrder.setFIamid(orderVo.getFIamid());
        mainOrder.setFWhid(orderVo.getFWhid());
        mainOrder.setFPamid(orderVo.getFPamid());
        mainOrder.setFPmid(orderVo.getFPmid());
        mainOrder.setFPid(orderVo.getFPid());
        mainOrder.setFEid(orderVo.getFEid());
        mainOrder.setFIamdate(orderVo.getFIamdate());
        mainOrder.setFIamstatus(orderVo.getFIamstatus());
        mainOrder.setFIamconfirm(orderVo.getFIamconfirm());
        mainOrder.setFIammemo(orderVo.getFIammemo());
        return mainOrder;
    }

    // 获取子表数据
    public List<InspectionArrivalChildOrder> getChildOrder(InspectionArrivalOrderVo orderVo){
        List<InspectionArrivalChildOrder> childOrderList = new ArrayList<>();
        InspectionArrivalChildOrder childOrder_temp = new InspectionArrivalChildOrder();
        for (int i=0;i<orderVo.getInspectionArrivalChildOrderVoList().size();i++){
            childOrder_temp.setFIamid(orderVo.getFIamid());
            childOrder_temp.setFMid(orderVo.getInspectionArrivalChildOrderVoList().get(i).getFMid());
            childOrder_temp.setFIacquantity(orderVo.getInspectionArrivalChildOrderVoList().get(i).getFIacquantity());
            childOrder_temp.setFIacdate(orderVo.getInspectionArrivalChildOrderVoList().get(i).getFIacdate());
            childOrder_temp.setFQualified(orderVo.getInspectionArrivalChildOrderVoList().get(i).getFQualified());
            childOrder_temp.setFUnqualified(orderVo.getInspectionArrivalChildOrderVoList().get(i).getFUnqualified());
            childOrder_temp.setFIacstatus(orderVo.getInspectionArrivalChildOrderVoList().get(i).getFIacstatus());
            childOrder_temp.setFIacmemo(orderVo.getInspectionArrivalChildOrderVoList().get(i).getFIacmemo());
            childOrderList.add(childOrder_temp);
        }
        return  childOrderList;
    }

    // 获取Vo
    public InspectionArrivalOrderVo getVo(InspectionArrivalMainOrderVo mainOrderVo,List<InspectionArrivalChildOrderVo> childOrderVos){
        InspectionArrivalOrderVo orderVo = new InspectionArrivalOrderVo();
        orderVo.setFIamid(mainOrderVo.getFIamid());
        orderVo.setFWhid(mainOrderVo.getFWhid());
        orderVo.setFWhname(mainOrderVo.getFWhname());
        orderVo.setFPamid(mainOrderVo.getFPamid());
        orderVo.setFPid(mainOrderVo.getFPid());
        orderVo.setFPname(mainOrderVo.getFPname());
        orderVo.setFPmid(mainOrderVo.getFPmid());
        orderVo.setFEid(mainOrderVo.getFEid());
        orderVo.setFEname(mainOrderVo.getFEname());
        orderVo.setFIamdate(mainOrderVo.getFIamdate());
        orderVo.setFIamstatus(mainOrderVo.getFIamstatus());
        orderVo.setFIamconfirm(mainOrderVo.getFIamconfirm());
        orderVo.setFIammemo(mainOrderVo.getFIammemo());
        orderVo.setInspectionArrivalChildOrderVoList(childOrderVos);
        return orderVo;
    }

}
